package cn.rongdao.jrkabao.ui.cashadvance.uicomponent;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mifengkong.frtools.util.FRDisplayMetrics;
import com.mifengkong.frtools.util.FRImageLoad;
import com.mifengkong.frtools.util.FRString;

import cn.rongdao.jrkabao.R;
import cn.rongdao.jrkabao.util.CommonUtil;

/**
 * Created by jack on 2017/9/9
 */

public class ItemBankCardUIComponent extends LinearLayout {
    public ItemBankCardUIComponent(Context context) {
        super(context);
    }

    public ItemBankCardUIComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ItemBankCardUIComponent(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private TextView tvCardName;
    private TextView tvAddCardName;
    private ImageView ivAddCard;
    private TextView tvChange;

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        tvCardName = findViewById(R.id.libc_tv_card_name);
        tvAddCardName = findViewById(R.id.libc_tv_add_card_name);
        ivAddCard = findViewById(R.id.libc_iv_add);
        tvChange = findViewById(R.id.libc_tv_change);
    }

    public void setCardName(String cardName) {
        tvCardName.setText(cardName);
    }

    public void setCashGoPayCardName(String cardName, String image) {
        tvAddCardName.setText(cardName);
        tvAddCardName.setTextColor(ContextCompat.getColor(getContext(), R.color.c666666));
        tvAddCardName.setTextSize(15);
        setImageViewSize(16.0f);
        FRImageLoad.loadImage(image, ivAddCard);
    }

    public void setAddCardName(String addCardName, String image, int flag) {
        if (!FRString.isEmpty(image)) {
            setCashGoPayCardName(addCardName, image);
            if (CommonUtil.isStatusSuccess(flag)) {
                tvChange.setVisibility(View.VISIBLE);
            }
        } else {
            if (CommonUtil.isStatusSuccess(flag)) {
                tvAddCardName.setText("添加信用卡号");
            } else {
                tvAddCardName.setText("添加储蓄卡号");
            }
            ivAddCard.setImageResource(R.drawable.icon_add_card);
            setImageViewSize(16.0f);
            tvAddCardName.setTextColor(ContextCompat.getColor(getContext(), R.color.cb0b0b0));
            tvAddCardName.setTextSize(13);
        }
    }

    private void setImageViewSize(float size) {
        LayoutParams lp = (LayoutParams) ivAddCard.getLayoutParams();
        lp.width = FRDisplayMetrics.dp2px(getContext(), size);
        lp.height = FRDisplayMetrics.dp2px(getContext(), size);
        ivAddCard.setLayoutParams(lp);
    }
}
